<style type="text/css">
    @import "../../assets/iconfont/iconfont.css";
    .ml-10{
        margin-left: 10px;
    }
    .mr-10{
        margin-right: 10px;
    }

</style>
<template>
    <div class="tree-menu">
        <h4>组织结构管理</h4>
        <Alert show-icon>
            此处用于管理智慧营区的组织结构，如单位、组织、部门等。
        </Alert>
        <Alert show-icon type="warning">
            需要在“智慧营区”中指定第一个级别为单位。
        </Alert>
        <ButtonGroup>
            <Button type="success" @click="addIsShow=true" :disabled="addBtnDisable">
                <Icon type="md-add-circle"/>
                添加
            </Button>
            <Button type="warning" :disabled="delBtnDisable" @click="deleteEvent(orgId,ordHaveNext)">
                <Icon type="md-close-circle"/>
                删除
            </Button>
            <Button type="primary" :disabled="editBtnDisable" @click="editClick(orgId)">
                <Icon type="md-close-circle"/>
                编辑
            </Button>
        </ButtonGroup>

        <Row>
            <Col span="8">
                <Alert size="large" type="warning" style="margin-top:15px;" show-icon>
                    当前选中：<b>{{editName}}</b>
                </Alert>
            </Col>
        </Row>

        <Row>
            <Col span="8">
                <Alert size="large" style="margin-top:15px;" show-icon>
                    <i class="iconfont iconbumen "></i> <span>单位</span>
                    <i class="iconfont iconrenyuan "></i> <span>部门</span>
                </Alert>
            </Col>
        </Row>

        <vue2-org-tree :data="tempTreeData" @on-node-click="orgChartClickEvent" :renderContent="renderTreeContent"/>

        <!-- 添加-->
        <Modal v-model="addIsShow" :title="editName"
               @on-cancel="addIsShow =false" @on-ok="handleSubmitAdd('addFormValidate')">
            <Form ref="addFormValidate" :model="addFormValidate" :label-width="100">
                <Form-item label="部门/单位名字" prop="Title">
                    <i-input v-model="addFormValidate.Title" placeholder="请输入组织名字"></i-input>
                </Form-item>
                <Form-item label="单位/组织" prop="Orgtype">
                    <Radio-group v-model="addFormValidate.Orgtype">
                        <Radio label="false">部门</Radio>
                        <Radio label="true">单位</Radio>
                    </Radio-group>
                </Form-item>
                <Form-item label="是否为车辆管理部门" prop="Isautomgr">
                    <Radio-group v-model="addFormValidate.Isautomgr">
                        <Radio label="true">是</Radio>
                        <Radio label="false">否</Radio>
                    </Radio-group>
                </Form-item>
            </Form>
        </Modal>

        <!--编辑-->
        <Modal v-model="editIsShow" title="编辑" @on-ok="handleSubmitEdit('editFormValidate')"
               @on-cancel="editIsShow =false">
            <Form ref="editFormValidate" :model="editFormValidate" :label-width="100">
                <Form-item label="部门/单位名字" prop="Title">
                    <i-input v-model="editFormValidate.Title" placeholder="请输入组织名字"></i-input>
                </Form-item>
                <Form-item label="单位/组织" prop="Orgtype">
                    <Radio-group v-model="editFormValidate.Orgtype">
                        <Radio label="false">部门</Radio>
                        <Radio label="true">单位</Radio>
                    </Radio-group>
                </Form-item>
                <Form-item label="是否为车辆管理部门" prop="Isautomgr">
                    <Radio-group v-model="editFormValidate.Isautomgr">
                        <Radio label="true">是</Radio>
                        <Radio label="false">否</Radio>
                    </Radio-group>
                </Form-item>
            </Form>
        </Modal>
    </div>
</template>
<script>

    import apiUtil from '../../assets/utils/api_util';
    import cTree from '../../components/trees'
    import axios from 'axios'
    import OrganizationChart from 'vue-organization-chart'
    import 'vue-organization-chart/dist/orgchart.css'
    import TreeChart from "vue-tree-chart";
    import baseUtil from "../../assets/utils/base-util";

    export default {
        data() {
            return {
                data: '',
                //添加
                addFormValidate: {
                    //添加字段
                    Isautomgr: 'false',
                    Title: '',
                    Orgtype: 'false',
                    Previd: '',
                },
                addIsShow: false,
                //编辑
                editFormValidate: {
                    //添加字段
                    Isautomgr: 'false',
                    Title: '',
                    Orgtype: 'false',
                    Previd: '',
                },
                editIsShow: false,
                tempTreeData: {
                    id: '',
                    label: ' 智慧营区',
                    orgtype:1,
                    children: []
                },
                editName: '点击需要操作的组织',
                orgId: '',
                ordHaveNext: '',
                unitOrOrg: '',
                // 操作按钮状态
                addBtnDisable: true,
                editBtnDisable: true,
                delBtnDisable: true,
            }
        },

        components: {
            cTree,
            OrganizationChart,
            TreeChart
        },
        created() {
            this.getData();
        },
        methods: {
            renderTreeContent: function (h, data) {

                // let icon_bm =require('/src/assets/image/org_bumen.png');
                let icon_dw ='http://img3.imgtn.bdimg.com/it/u=1985297934,244112715&fm=26&gp=0.jpg';

                let labelClassName ='iconfont '

                // let htmlItem =   ' (<b>{data.label}</b>)'
                // return htmlItem

                let className ='';

                if(data.orgtype ==1 ){
                    className="iconfont iconbumen mr-10"
                }else {
                    className="iconfont iconrenyuan mr-10"
                }

                console.log('h,',h)
                let htmlItem = h('div',
                    [
                        h('i',{'class':className}),
                        h('b', data.label),
                        h('br'),
                    ]
                );
                return htmlItem;
            },
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id.length > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            //添加
            handleSubmitAdd(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.addData(model);
                });
            },
            addData: function (model) {
                let that = this;

                console.log('model:', model)
                let para = {
                    Title: model.Title,
                    Previd: that.orgId,
                    Isautomgr: model.Isautomgr,
                }
                let params = that.$qs.stringify(para);
                this.$Loading.start();


                if (model.Orgtype == 'true') {
                    apiUtil.post(that, apiUtil.urls.UnitCreate, params, function (res) {
                        that.$Loading.finish();
                        that.addModal = false;
                        that.getData();
                        that.$Message.info('添加成功 ')
                    });
                } else {
                    apiUtil.post(that, apiUtil.urls.OrganizeCreate, params, function (res) {
                        that.$Loading.finish();
                        that.addModal = false;
                        that.getData();
                        baseUtil.handleReset(that,'addFormValidate');
                        that.$Message.info('添加成功 ')
                    });
                }

            },
            // 检查操作按钮显示状态
            checkOpBtnState: function () {
                if (this.editName === "点击需要操作的组织") {
                    this.addBtnDisable = true;
                    this.delBtnDisable = true;
                    this.editBtnDisable = true;
                } else {
                    this.addBtnDisable = false;
                    this.delBtnDisable = false;
                    this.editBtnDisable = false;
                }
            },
            orgChartClickEvent: function (e, nodeData) {
                this.editName = nodeData.label;
                this.orgId = nodeData.id;
                this.ordHaveNext = nodeData.children;
                if (nodeData.orgtype == '1') {
                    this.unitOrOrg = 'unit';
                } else {
                    this.unitOrOrg = 'org';
                }

                this.checkOpBtnState();
                console.log('data:', nodeData);
                // console.log('e:' ,e);

            },
            //获取详情
            getDetail: function (id) {
                let that = this;
                that.$Loading.start();

                let para = {
                    id: id,
                };

                apiUtil.get(that, apiUtil.urls.OrganizeDetail, para, function (res) {
                    that.$Loading.finish();
                    that.editFormValidate.id = res.Msg.Id;
                    that.editFormValidate.Title = res.Msg.Title;
                    that.editFormValidate.Isautomgr = res.Msg.Isautomgr.toString();
                    if (res.Msg.Orgtype == 1) {
                        that.editFormValidate.Orgtype = 'true';
                    }else {
                        that.editFormValidate.Orgtype = 'false';

                    }
                    that.editFormValidate.Previd = res.Msg.Previd;
                });
            },
            //删除组织
            deleteEvent: function (id, index) {

                console.log('id', id)
                let that = this;

                if (index.length > 0) {

                    this.$Notice.warning({
                        top: 50,
                        duration: 3,
                        title: '不能删除有下级组织结构的组织'
                    })
                } else {

                    this.$Modal.confirm({
                        title: '提示',
                        content: `是否删除该组织吗`,
                        onOk: function () {
                            this.$Loading.start();
                            let para = {
                                id: id
                            };
                            let params = that.$qs.stringify(para);

                            let apiUrl = "";

                            if (that.unitOrOrg == 'org') {
                                apiUrl = apiUtil.urls.OrganizeDelete
                            } else {
                                apiUrl = apiUtil.urls.UnitDelete
                            }

                            axios.post(apiUrl, params).then(response => {

                                let res = response.data;

                                if (res.State) {
                                    that.$Message.success('删除成功 ');
                                    that.getData();
                                }

                            }).catch(error => {
                                if (error.response) {
                                    // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                                } else {
                                    // Something happened in setting up the request that triggered an Error
                                }
                            });

                            // apiUtil.post(that, apiUtil.urls.OrganizeDelete, params, function (res) {
                            //     that.$message.success('删除成功 ');
                            //     that.getData();
                            //     console.log('删除过后返回：',res)
                            // });
                        }
                    })
                }

            },
            getData: function () {
                let that = this;

                let para = {
                    // uplevelid: ''
                };

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.OrgTreeAll, para, function (res) {
                    that.tempTreeData.children = res.Msg;
                });

            },
            editClick: function (id) {
                this.editIsShow = true;
                this.getDetail(id);
            },

            handleSubmitEdit(name) {
                let that = this;
                let model = this.$refs[name].model;
                // this.$refs[name].validate((valid) => {
                that.comfirmModifyPSEdit(model);
                //     console.log('111')
                //
                // })
            },

            comfirmModifyPSEdit: function (model) {
                let that = this;
                console.log('111')

                let para = {
                    Title: model.Title,
                    Previd: model.Previd,
                    Id: model.id,
                    Isautomgr: model.Isautomgr,
                };

                let params = that.$qs.stringify(para);

                let apiUrl = "";

                if (that.unitOrOrg == 'org') {
                    apiUrl = apiUtil.urls.OrganizeEdit
                } else {
                    apiUrl = apiUtil.urls.UnitEdit
                }

                apiUtil.post(that, apiUrl, params, function (res) {
                    that.$Message.info('修改成功 ');

                    that.getData();

                });
            },
        }
    }
</script>
